/**
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 *
 * The Initial Developer of the Original Code is sephiroth.it
 *
 * Contributor(s):
 *   Gabriele Farina <gabriele@sephiroth.it>
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 */
package it.sephiroth.gettext
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.utils.ByteArray;
	
	/**
	 * The <code>GetText</code> class is a global singleton used to
	 * manage translations during the application lifetime.
	 * 
	 * Every time a new language is installed, a <code>change</code>
	 * event is dispatched so an application can react accordling.
	 * 
	 * <code>GetText</code> should never be used directly (but when
	 * someone wants to listen to <code>change</code> events); the
	 * corresponding <code>_</code> and <code>install</code> functions
	 * should be used.
	 * 
	 * @author Gabriele Farina <gabriele@sephiroth.it>
	 */
	[Event(name="change", type="flash.events.Event")]
	public final class GetText extends EventDispatcher
	{
		/**
		 * Singleton instance 
		 */
		private static var _instance: GetText = null;
		
		/**
		 * Retrieve the shared instance of the <code>GetText</code>
		 * singleton, or create it if it doesn't exist.
		 * 
		 * @return Instance of the <code>GetText</code> class.
		 * 
		 */
		public static function getInstance(): GetText
		{
			if( _instance == null )
			{
				_instance = new GetText( new SingletonCheck() );
			}
			
			return _instance;
		}
		
		private var _domain: String;
		private var _language: String;
		private var _translation: Translation;
		
		/**
		 * Instanciate the <code>GetText</code> class.
		 * 
		 * As long as <code>GetText</code> is a singleton, the constructor
		 * should never be used directly. To force this, an instance of a
		 * private class must be passed as first argument.
		 * 
		 * @param	check	Instance of the internal class to be used as a check.
		 * 
		 */
		public function GetText( check: SingletonCheck = null )
		{
			super( this );
			
			if( check == null )
			{
				throw new Error( "it.sephiroth.gettext.GetText is a singleton and cannot be instancied using new. Call GetText.getInstance() to retrieve an instance!" );
			}
			
			_translation = new DummyTranslation();
		}
		
		/**
		 * Current installed language, or null. 
		 */
		public function get language(): String
		{
			return _language;
		}
		
		/**
		 * Current installed domain, or null. 
		 */
		public function get domain(): String
		{
			return _domain;
		}
		
		/**
		 * @see	it.sephiroth.gettext.install
		 */
		public function install( domain: String, language: String, data: ByteArray ): void
		{
			if( ( _domain == domain ) && ( _language == language ) )
			{
				return;
			}
			
			_domain = domain;
			_language = language;
			
			_translation = Translation.fromByteArray( data );
			
			dispatchEvent( new Event( Event.CHANGE ) );
		}
		
		/**
		 * @see	it.sephiroth.gettext._
		 */
		public function translate( k: String ): String
		{
			return _translation.getTranslation( k );
		}

	}
}

import it.sephiroth.gettext.Translation;
import flash.utils.Dictionary;

/**
 * Private class used to assure that <code>GetText</code> is
 * never constructed directly.
 * 
 * @author Gabriele Farina <gabriele@sephiroth.it>
 * 
 */
class SingletonCheck {}

/**
 * Dummy translation used when no translations have been
 * installed.
 * 
 * @author Gabriele Farina <gabriele@sephiroth.it>
 * 
 */
class DummyTranslation extends Translation
{
	public function DummyTranslation()
	{
		super( new Dictionary(), new Dictionary() );
	}
	
	/**
	 * @inheritDoc
	 */
	override public function get charset(): String
	{
		return "";
	}
	
	/**
	 * @inheritDoc
	 */
	override public function getInformation( k: String ): String
	{
		return null;
	}
	
	/**
	 * @inheritDoc
	 */
	override public function getTranslation( k: String ): String
	{
		return k;
	}
}